

import { Node } from "cc";
import { GComponent, GObject } from "fairygui-cc";
import { IComponent } from "../common/IComponent";
import { UIMediator } from "./UIMediator";

export class UIComponent extends UIMediator implements IComponent {

    protected parent!: GComponent;

    /**
     * 设置父节点
     * @param target 
     * @param args
     * @param isLoad
     */
    async init(target: GObject | Node, args: any, isLoad?: boolean) {

        if (isLoad) {
            if (target instanceof GComponent) {
                this.parent = target;
                await this.start(args);
            }
            else {
                console.error("The target must be <GComponent>");
            }
        }
        else {
            this.setParameter(args);
            this.attach(target);
        }
    }

    destroy() {
        
    }

    /**
     * 关联表现对象
     */
    protected onAttach() {
        super.onAttach();
        if (this.parent && this.view)
            this.parent.addChild(this.view);
    }

    /**
     * 显示在最前面，注意是当前层级的
     */
    public toFront() {
        if (this.view) {
            let i = this.view.parent._children.length;
            this.view.node.setSiblingIndex(i);
        }
    }

    /**
     * 显示在最后面，注意是当前层级的
     */
    public toBack() {
        if (this.view) {
            this.view.node.setSiblingIndex(0);
        }
    }

    /**
     * 全面显示
     */
    public makeFullScreen() {
        if (this.view)
            this.view.makeFullScreen();
    }
}